package dominion.model.actions.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import dominion.model.Card;
import dominion.model.Deck;
import dominion.model.GameBoard;
import dominion.model.Player;
import dominion.model.actions.Action;
import dominion.utils.Constants;

public class Feast extends Action {

	public Feast() {
		cost = 4;
	}

	@Override
	public Card clone() {
		return new Feast();
	}

	@Override
	public String toString() {
		return "Feast";
	}

	@Override
	public String getDescription() {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDescription());
		sb.append("Trash this card.\n");
		sb.append("Gain a card costing up to $5.\n");
		return sb.toString();
	}

	@Override
	public void doAction(Player player, GameBoard board, List<Player> players) throws IOException {
		player.changeAction(-1);
		// Can assume its there or this wouldn't be played
		Card current = player.drawFromDeck(toString(), Constants.HAND);
		board.addToTrash(current);

		Constants.printer.print("Pick a card to gain costing up to $5.");
		Constants.printer.printBoard(board);
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String choice = null;
		while (true)
		{
			choice = in.readLine().trim().toLowerCase();
			Card card = Constants.cardMap.get(choice);
			if (card == null)
			{
				Constants.printer.print("Invalid card.");
			}
			else if (card.getCost() > 5)
			{
				Constants.printer.print("Must pick a card costing at most $5.");
			}
			else
			{
				Deck deck = board.getMap().get(choice);
				player.addToDeck(deck.drawTopCard(), Constants.DISCARD_DECK);
				break;
			}
		}
	}
}
